/* 演示 promise<int> 在线程间传递结果

*/

#include <chrono>
#include <future>
#include <iostream>
#include <numeric>
#include <thread>
#include <vector>

void add_fun(int a,int b,
                std::promise<int> accumulate_promise)
{
    int sum = a + b;
    accumulate_promise.set_value(sum); // 提醒 future
}

int main()
{
    std::promise<int> accumulate_promise;
    std::future<int> accumulate_future = accumulate_promise.get_future();   // promise 与 future 关联
    std::thread work_thread(add_fun, 11, 22, std::move(accumulate_promise));

    // future::get() 将等待直至该 future 拥有合法结果并取得它
    // 无需在 get() 前调用 wait()
    // accumulate_future.wait(); // 等待结果
    std::cout << "result=" << accumulate_future.get() << '\n';
    work_thread.join(); // wait for thread completion

    return 0;
}